#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "../include/bomb.h"
#include "../include/map.h"
#include "../include/constant.h"
#include "../include/player.h"
#include "../include/monster_list.h"
#include "../include/monster.h"

struct s_bomb
{
	int x, y;
	int power;
	Uint32 time_;
	SDL_Surface * directed_img[7];
	int bomb_lifetime;
	int has_explode;

	enum e_time_bomb current_timebomb;
	enum e_explode_bomb current_explodebomb;
};

//create new bomb
t_bomb bomb_new(int x, int y, int power)
{
	t_bomb bomb = malloc (sizeof(*bomb));
	bomb->x = x;
	bomb->y = y;
	bomb->power = power;
	bomb->time_ = SDL_GetTicks();
	bomb->bomb_lifetime = BOMB_LIFETIME;
	bomb->has_explode = 0;

	bomb->directed_img[TIMEBOMB_1] = load_image(IMG_BOMB_TTL1);
	bomb->directed_img[TIMEBOMB_2] = load_image(IMG_BOMB_TTL2);
	bomb->directed_img[TIMEBOMB_3] = load_image(IMG_BOMB_TTL3);
	bomb->directed_img[TIMEBOMB_4] = load_image(IMG_BOMB_TTL4);
	bomb->directed_img[EXPLODEBOMB_0] = load_image(IMG_BOMBEXPLODE_0);
	bomb->directed_img[EXPLODEBOMB_1] = load_image(IMG_BOMBEXPLODE_1);
	bomb->directed_img[EXPLODEBOMB_2] = load_image(IMG_BOMBEXPLODE_2);

	bomb->current_timebomb = TIMEBOMB_4;
	bomb->current_explodebomb = EXPLODEBOMB_1;
	return bomb;
}

int bomb_get_power(t_bomb bomb){
	assert(bomb);
	return bomb->power;
}

void bomb_inc_range(t_bomb bomb) {
	assert(bomb);
	if(bomb->power < 10)
		bomb->power += 1;
}

void bomb_dec_range(t_bomb bomb) {
	assert(bomb);
	if(bomb->power)
		bomb->power -= 1;
}

void bomb_set_current_timebomb(t_bomb bomb, enum e_time_bomb timebomb)
{
	assert(bomb);
	bomb->current_timebomb = timebomb;
}

int bomb_display(t_bomb bomb, SDL_Surface *screen, t_map map, t_player player,t_list monster_list,t_monster monster){
	assert(bomb);
	assert(screen);
	assert(map);
	assert(player);

	SDL_Rect place;			//where the player is
	place.x = bomb->x * SIZE_BLOC;
	place.y = bomb->y * SIZE_BLOC;

	SDL_Rect place1;		//top
	place1.x = bomb->x * SIZE_BLOC;
	place1.y = (bomb->y -1) * SIZE_BLOC;

	SDL_Rect place2;		//down
	place2.x = bomb->x * SIZE_BLOC;
	place2.y = (bomb->y +1) * SIZE_BLOC;

	SDL_Rect place3;		//right
	place3.x = (bomb->x +1)* SIZE_BLOC;
	place3.y = bomb->y * SIZE_BLOC;

	SDL_Rect place4;		//left
	place4.x = (bomb->x -1)* SIZE_BLOC;
	place4.y = bomb->y * SIZE_BLOC;

	int k = rand()%60;

	SDL_BlitSurface(bomb->directed_img[bomb->current_timebomb],NULL, screen, &place);

	Uint32 diff1 = SDL_GetTicks() - bomb->time_;
	if( diff1 > 1000 && diff1 <2000){
		bomb_set_current_timebomb(bomb, TIMEBOMB_3);
	}
	if( diff1 > 2000 && diff1 <3000){
		bomb_set_current_timebomb(bomb, TIMEBOMB_2);
	}
	if( diff1 > 3000 && diff1 <4000){
		bomb_set_current_timebomb(bomb, TIMEBOMB_1);
	}
	if ( diff1 > 4000 && diff1 <4050){
		bomb_set_current_timebomb(bomb, EXPLODEBOMB_0 );
	}

	if (diff1 > 4100 && diff1 < 4400 ){
		bomb->has_explode = 1;

		if(map_get_cell_type(map, bomb->x, bomb->y) == CELL_PLAYER && (diff1 > 4200 && diff1 < 4400) && player_get_nb_lifes(player) <10)
			player_dec_nb_lifes(player);

		if(map_get_cell_type(map, bomb->x, bomb->y) == CELL_MONSTER){		//if monster is on the bomb it disappear
			map_set_cell_type(map, bomb->x, bomb->y, CELL_EMPTY);
			monster_list_delete(monster_list,monster);
		}

		for(int i=1; i <= player_get_range_bomb(player); i++){									//top
			place1.x = bomb->x * SIZE_BLOC;
			place1.y = (bomb->y -i) * SIZE_BLOC;

			if(map_get_cell_type(map, bomb->x, bomb->y-i) == CELL_SCENERY)
				break;

			if(map_get_cell_type(map, bomb->x, bomb->y-i) == CELL_PLAYER && (diff1 > 4200 && diff1 < 4400) && player_get_nb_lifes(player) <10)
				player_dec_nb_lifes(player);

			if(map_get_cell_type(map, bomb->x, bomb->y-i) == CELL_CASE){
				if(k<10) map_set_cell_type(map, bomb->x, bomb->y-i, CELL_BONUS | BONUS_BOMB_RANGE_INC << 4);
				if(k<20 && k>11) map_set_cell_type(map, bomb->x, bomb->y-i, CELL_BONUS | BONUS_BOMB_RANGE_DEC << 4);
				if(k<30 && k>21) map_set_cell_type(map, bomb->x, bomb->y-i, CELL_BONUS | BONUS_BOMB_NB_INC << 4);
				if(k<40 && k>31) map_set_cell_type(map, bomb->x, bomb->y-i, CELL_BONUS | BONUS_BOMB_NB_DEC << 4);
				if(k<50 && k>41) map_set_cell_type(map, bomb->x, bomb->y-i, CELL_BONUS | BONUS_LIFE << 4);
				if(k<60 && k>51) map_set_cell_type(map, bomb->x, bomb->y-i, CELL_EMPTY);
			}

			if(map_get_cell_type(map, bomb->x, bomb->y-i) == CELL_MONSTER){
				map_set_cell_type(map, bomb->x, bomb->y-i, CELL_EMPTY);
				monster_list_delete(monster_list,monster);
			}
			SDL_BlitSurface(bomb->directed_img[EXPLODEBOMB_1],NULL, screen, &place1); //top
		}

		for(int i=1; i <= player_get_range_bomb(player); i++){									//down
			place2.x = bomb->x * SIZE_BLOC;
			place2.y = (bomb->y +i) * SIZE_BLOC;

			if(map_get_cell_type(map, bomb->x, bomb->y+i) == CELL_SCENERY || map_is_inside(map, bomb->x, bomb->y+i)==0)
				break;

			if(map_get_cell_type(map, bomb->x, bomb->y+i) == CELL_PLAYER && (diff1 > 4200 && diff1 < 4400) && player_get_nb_lifes(player) <10)
				player_dec_nb_lifes(player);

			if(map_get_cell_type(map, bomb->x, bomb->y+i) == CELL_CASE){
				if(k<10) map_set_cell_type(map, bomb->x, bomb->y+i, CELL_BONUS | BONUS_BOMB_RANGE_INC << 4);
				if(k<20 && k>11) map_set_cell_type(map, bomb->x, bomb->y+i, CELL_BONUS | BONUS_BOMB_RANGE_DEC << 4);
				if(k<30 && k>21) map_set_cell_type(map, bomb->x, bomb->y+i, CELL_BONUS | BONUS_BOMB_NB_INC << 4);
				if(k<40 && k>31) map_set_cell_type(map, bomb->x, bomb->y+i, CELL_BONUS | BONUS_BOMB_NB_DEC << 4);
				if(k<50 && k>41) map_set_cell_type(map, bomb->x, bomb->y+i, CELL_BONUS | BONUS_LIFE << 4);
				if(k<60 && k>51) map_set_cell_type(map, bomb->x, bomb->y+i, CELL_EMPTY);
			}

			if(map_get_cell_type(map, bomb->x, bomb->y+i) == CELL_MONSTER){
				map_set_cell_type(map, bomb->x, bomb->y+i, CELL_EMPTY);
				monster_list_delete(monster_list,monster);
			}
			SDL_BlitSurface(bomb->directed_img[EXPLODEBOMB_1],NULL, screen, &place2); //down
		}

		for(int i=1; i <= player_get_range_bomb(player); i++){									//right
			place3.x = (bomb->x +i)* SIZE_BLOC;
			place3.y = bomb->y * SIZE_BLOC;

			if(map_get_cell_type(map, bomb->x+i, bomb->y) == CELL_SCENERY)
				break;

			if(map_get_cell_type(map, bomb->x+i, bomb->y) == CELL_PLAYER && (diff1 > 4200 && diff1 < 4400) && player_get_nb_lifes(player) <10)
				player_dec_nb_lifes(player);

			if(map_get_cell_type(map, bomb->x+i, bomb->y) == CELL_CASE){
				if(k<10) map_set_cell_type(map, bomb->x+i, bomb->y, CELL_BONUS | BONUS_BOMB_RANGE_INC << 4);
				if(k<20 && k>11) map_set_cell_type(map, bomb->x+i, bomb->y, CELL_BONUS | BONUS_BOMB_RANGE_DEC << 4);
				if(k<30 && k>21) map_set_cell_type(map, bomb->x+i, bomb->y, CELL_BONUS | BONUS_BOMB_NB_INC << 4);
				if(k<40 && k>31) map_set_cell_type(map, bomb->x+i, bomb->y, CELL_BONUS | BONUS_BOMB_NB_DEC << 4);
				if(k<50 && k>41) map_set_cell_type(map, bomb->x+i, bomb->y, CELL_BONUS | BONUS_LIFE << 4);
				if(k<60 && k>51) map_set_cell_type(map, bomb->x+i, bomb->y, CELL_EMPTY);
			}

			if(map_get_cell_type(map, bomb->x+i, bomb->y) == CELL_MONSTER){
				map_set_cell_type(map, bomb->x+i, bomb->y, CELL_EMPTY);
				monster_list_delete(monster_list,monster);
			}
			SDL_BlitSurface(bomb->directed_img[EXPLODEBOMB_2],NULL, screen, &place3); //right
		}

		for(int i=1; i <= player_get_range_bomb(player); i++){									//left
			place4.x = (bomb->x -i)* SIZE_BLOC;
			place4.y = bomb->y * SIZE_BLOC;

			if(map_get_cell_type(map, bomb->x-i, bomb->y) == CELL_SCENERY)
				break;

			if(map_get_cell_type(map, bomb->x-i, bomb->y) == CELL_PLAYER && (diff1 > 4200 && diff1 < 4400) && player_get_nb_lifes(player) <10)
				player_dec_nb_lifes(player);

			if(map_get_cell_type(map, bomb->x-i, bomb->y) == CELL_CASE){
				if(k<10) map_set_cell_type(map, bomb->x-i, bomb->y, CELL_BONUS | BONUS_BOMB_RANGE_INC << 4);
				if(k<20 && k>11) map_set_cell_type(map, bomb->x-i, bomb->y, CELL_BONUS | BONUS_BOMB_RANGE_DEC << 4);
				if(k<30 && k>21) map_set_cell_type(map, bomb->x-i, bomb->y, CELL_BONUS | BONUS_BOMB_NB_INC << 4);
				if(k<40 && k>31) map_set_cell_type(map, bomb->x-i, bomb->y, CELL_BONUS | BONUS_BOMB_NB_DEC << 4);
				if(k<50 && k>41) map_set_cell_type(map, bomb->x-i, bomb->y, CELL_BONUS | BONUS_LIFE << 4);
				if(k<60 && k>51) map_set_cell_type(map, bomb->x-i, bomb->y, CELL_EMPTY);
			}

			if(map_get_cell_type(map, bomb->x-i, bomb->y) == CELL_MONSTER){
				map_set_cell_type(map, bomb->x-i, bomb->y, CELL_EMPTY);
				monster_list_delete(monster_list,monster);
			}
			SDL_BlitSurface(bomb->directed_img[EXPLODEBOMB_2],NULL, screen, &place4); //left
		}
		bomb->bomb_lifetime = 0;
	}
	int x = 2;
	if(diff1 > 4200 && diff1 < 4500){
		if(bomb->bomb_lifetime == 0) x=0;
		else x=1;
	}

	return x;
}

Uint32 bomb_get_time(t_bomb bomb){
	assert(bomb);

	return bomb->time_;
}


void bomb_set_time(t_bomb bomb, Uint32 time){
	assert(bomb);

	bomb->time_=time;
}
